﻿using SubSpec;
using Xunit;

namespace Ecos.GroceryList.Domain.Test
{
    public class ProductSpecs
    {
        [Specification]
        public void Creation()
        {
            string item = null;
            Product product = null;
            
            "Given: A product name".Context(() => 
                item = "milk");

            "When: I create a product".Do(() => 
                product = Product.From(item));

            "Then: The product is created (obvious)".Assert(() => 
                Assert.IsType<Product>(product));
        }
        [Specification]
        public void Identification()
        {
            string item = null;
            Product product = null;

            Product product1 = null;
            Product productCreatedWithTheSameItem = null;
            Product productCreatedWithDifferentItem = null;
            "Given: Two products created with the same name, and another one created with different name".Context(() =>{
                const string oneItem = "milk";
                const string anotherItem = "coffee";
                product1 = Product.From(oneItem);
                productCreatedWithTheSameItem = Product.From(oneItem);
                productCreatedWithDifferentItem = Product.From(anotherItem);
            });

            "Then: The products created with the same item are equals".Assert(() =>
            {
                Assert.Equal(product1, productCreatedWithTheSameItem);
                Assert.True(product1.Equals(productCreatedWithTheSameItem));
            });

            "Then: The products created with the same item has the same hashCode".Assert(() =>
                Assert.True(product1.GetHashCode()==productCreatedWithTheSameItem.GetHashCode()));


            "Then: The products created with a different item are not equals".Assert(() =>
            {
                Assert.NotEqual(product1, productCreatedWithDifferentItem);
                Assert.False(product1.Equals(productCreatedWithDifferentItem));
            });


        }
    }
}
